import java.net.*;
import java.util.Vector;
import java.util.concurrent.LinkedBlockingQueue;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferedImage;
import java.io.*;

import javax.imageio.ImageIO;
import javax.swing.JFrame;
import javax.swing.JPanel;
import javax.swing.JTextArea;

public class Receiver extends Thread {
    /**
         * 
         */
        private static final long serialVersionUID = 1L;
        private ServerSocket accepter;
    Boolean destinationReceivedHeader;
    JTextArea chatArea;
    PaintGUI AboveGUI;
    ReceiverThread receiverThread;
    LinkedBlockingQueue<DrawingChange> drawingQueue;
    LinkedBlockingQueue<String> chatQueue;
    
    final int Message = 1;
    final int Picture = 2;
    final int Changes = 3;
    final int NewTab = 4;
    final int TextFile = 5;
    
    public Receiver(int port, JTextArea textArea, PaintGUI AboveGUI, LinkedBlockingQueue<DrawingChange> drawingQueue, LinkedBlockingQueue<String> chatQueue) throws IOException {
        this.AboveGUI = AboveGUI;
        this.chatArea = textArea;
        this.drawingQueue = drawingQueue;
        this.chatQueue = chatQueue;
        accepter = new ServerSocket(port);
        System.out.println("Server IP address: " + accepter.getInetAddress());
    }

    public void run() {
        try {
                        listen();
                } catch (IOException e) {
                        // TODO Auto-generated catch block
                        e.printStackTrace();
                }
    }
    
    public void listen() throws IOException {
        for (;;) {
                System.out.println("Before read");
                
                //int foreignPort = s.getPort();
                //InetAddress foreignAddress = s.getInetAddress();
                //Socket socketToTalkBack = new Socket(foreignAddress, foreignPort);
                
                //Get the header, if the header signifies that the message is a text message, then 
                //call the BufferedReader. If the message on the other hand is for an image then call the
                //image transfer protocol. Finally, if the transfer is for an array of changes, then
                //call that transfer protocol.
                
                Socket SocketConnection = accepter.accept();
                receiverThread = new ReceiverThread(drawingQueue, chatQueue, SocketConnection, AboveGUI);
        }
    }
}
